package com.cloudcms.web.common;

import com.cloudcms.web.util.Constants;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//全局异常捕捉处理
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public String errorHandler(Exception ex) {
        /*Map map = new HashMap();
        map.put("code", 400);
        //判断异常的类型,返回不一样的返回值
        if (ex instanceof MissingServletRequestParameterException) {
            map.put("msg", "缺少必需参数：" + ((MissingServletRequestParameterException) ex).getParameterName());
        } else if (ex instanceof BusinessException) {
            map.put("msg", "这是自定义异常");
        }*/
        Result result = new Result();
        if(ex instanceof BusinessException){
            result.setCode(((BusinessException) ex).getCode());
            result.setMsg(ex.getMessage());
        }else{
            ex.printStackTrace();
            result.setCode(Constants.SIMPLE_RESULT_SYSTEM);
            if(ex.getMessage()==null||"".equals(ex.getMessage()))
                result.setMsg("未分类错误");
            else
                result.setMsg(ex.getMessage());
        }
        return result.toString();
    }
}
